<?php

namespace app\controllers;

use app\models\Fbx;
use app\models\Image;
use app\models\UploadForm;
use Yii;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;

/**
 * Created by PhpStorm.
 * User: blacxhunt
 * Date: 2017/06/23
 * Time: 11:38
 */
class UploadController extends \yii\web\Controller
{
    private $UploadForm;
    private $ImageUrl;

    /**
     * @return $this|string
     * @throws \yii\base\Exception
     */
    function actionUpload()
    {
        $this->UploadForm = new UploadForm();
        $this->uploadImage();
        $this->uploadFbx();
        Yii::$app->response->redirect(['site/index']);
        return "success";
    }

    private function uploadImage()
    {
        if (Yii::$app->request->isPost) {
            $this->UploadForm->image = UploadedFile::getInstance($this->UploadForm, 'image');
            if ($this->UploadForm->image) {
                FileHelper::createDirectory('Images/');
                $name = $this->UploadForm->image->name;
                $isExisted = Image::find()->where(['name' => $name, 'author_id' => Yii::$app->user->getId()])->exists();
                if ($isExisted) {
                    $Image = Image::findOne(['name' => $name, 'author_id' => Yii::$app->user->getId()]);
                    $Image->version += 1;
                    $Image->img_url = $this->ImageUrl;
                    if ($this->UploadForm->image->saveAs('Images/' . $name)) {
                        $Image->update_date = date('Y-m-d H:i:s');
                        if (!$Image->save()) {
                            return $Image->errors;
                        }
                    }
                } else {
                    if ($this->UploadForm->image->saveAs('Images/' . $name)) {
                        $this->ImageUrl = "Images/" . $name;
                    }
                    $Image = new Image();
                    $Image->name = $name;
                    $Image->location = 'Images/' . $name;
                    $Image->type = "Images";
                    $Image->create_date = date('Y-m-d H:i:s');
                    $Image->update_date = date('Y-m-d H:i:s');
                    $Image->author_id = Yii::$app->user->getId();
                    $Image->img_url = $this->ImageUrl;
                    if (!$Image->save()) {
                        return $Image->errors;
                    }

                }
            }
            return "success";
        }
        return "false";
    }

    private function uploadFbx()
    {
        if (Yii::$app->request->isPost) {
            $this->UploadForm->fbx = UploadedFile::getInstance($this->UploadForm, 'fbx');
            if ($this->UploadForm->fbx) {
                $name = $this->UploadForm->fbx->name;
                FileHelper::createDirectory('FBX/');
                if (Fbx::find()->where(['name' => $name, 'author_id' => Yii::$app->user->getId()])->exists()) {
                    $Model = Fbx::findOne(['name' => $name, 'author_id' => Yii::$app->user->getId()]);
                    $Model->version += 1;
                    $Model->img_url = $this->ImageUrl;
                    if ($this->UploadForm->fbx->saveAs('FBX/' . $name)) {
                        $Model->update_date = date('Y-m-d H:i:s');
                        if (!$Model->save()) {
                            return $Model->errors;
                        }
                    }
                } else {
                    $this->UploadForm->fbx->saveAs('FBX/' . $name);
                    $FBXModel = new Fbx();
                    $FBXModel->name = $name;
                    $FBXModel->location = 'FBX/' . $name;
                    $FBXModel->type = "FBX";
                    $FBXModel->create_date = date('Y-m-d H:i:s');
                    $FBXModel->update_date = date('Y-m-d H:i:s');
                    $FBXModel->author_id = Yii::$app->user->getId();
                    $FBXModel->img_url = $this->ImageUrl;
                    if (!$FBXModel->save()) {
                        return $FBXModel->errors;
                    }
                }
            }
            return true;
        }
        return false;
    }
}
